/**
 * 
 */
package de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.tests;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import org.junit.Before;
import org.junit.Test;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.data.MultinomialOnlyCorpus;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.data.Word;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CSVTokenizer;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CreateFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.CreateGraph;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.FeatureType;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.CaseFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.datacreation.feature_types.PunctuationFeatures;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.learner.IncrementalViterbiTraining;
import de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.model.MultinomialWithLexicon;
/**
 * @author christoph
 *
 */
public class TestIncrementalViterbiTraining
{
	/**
	 * 
	 */
	private MultinomialWithLexicon mwl;
	/**
	 * 
	 */
	private MultinomialOnlyCorpus wc;
	/**
	 * 
	 */
	private IncrementalViterbiTraining ivt;
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{
		wc = new MultinomialOnlyCorpus();
		FeatureType[] ft = new FeatureType[] {new PunctuationFeatures(),
				new CaseFeatures()};
		makeFeaturePart(ft,wc);
		mwl = new MultinomialWithLexicon(wc, 0.1, 5, 0.1, 0.001, 0.1);
		ivt = new IncrementalViterbiTraining();
	}
	/**
	 * 
	 * @param ft
	 * @param wc 
	 * @throws IOException
	 */
	private void makeFeaturePart(FeatureType[] ft, MultinomialOnlyCorpus wc) throws IOException
	{
		CreateFeatures cf = new CreateFeatures(ft);
		StringReader sr = new StringReader(TestStrings.SIMPLE);
		BufferedReader br = new BufferedReader(sr);
		cf.addFeatures(br, new CSVTokenizer(false));
		br.close();
		addFeatureLexicon(ft,cf,wc);
		addWords(cf,ft,wc);
	}
	/**
	 * 
	 * @param cf
	 * @param ft 
	 * @param wc 
	 * @throws IOException
	 */
	private void addWords(CreateFeatures cf, FeatureType[] ft, MultinomialOnlyCorpus wc) throws IOException
	{
		StringReader sr;
		BufferedReader br;
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		cf.writeOutFeatures(bw);
		bw.close();
		sr = new StringReader(sw.toString());
		br = new BufferedReader(sr);
		CreateGraph cg = new CreateGraph(true, false, ft);
		StringReader sb = new StringReader(TestStrings.SIMPLE);
		BufferedReader bb = new BufferedReader(sb);
		cg.addAlignments(bb);
		bb.close();
		StringWriter sc = new StringWriter();
		BufferedWriter bc = new BufferedWriter(sc);
		cg.writeOutEdges(bc);
		bc.close();
		sb = new StringReader(sc.toString());
		bb = new BufferedReader(sb);
		wc.addWordsAndConnections(br, bb);
		br.close();
	}
	/**
	 * 
	 * @param ft
	 * @param cf
	 * @param wc 
	 * @throws IOException
	 */
	private void addFeatureLexicon(FeatureType[] ft, CreateFeatures cf, MultinomialOnlyCorpus wc) throws IOException
	{
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		cf.writeOutFeatureLexicon(bw);
		bw.close();
		StringReader sr = new StringReader(sw.toString());
		BufferedReader br = new BufferedReader(sr);
		wc.addFeatures(br);
	}
	/**
	 * Test method for {@link de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.learner.IncrementalViterbiTraining#train(de.uni_leipzig.informatik.asv.unsupervised_pos_tagger.model.Model, double, int)}.
	 * @throws IOException 
	 */
	@Test
	public void testTrain() throws IOException
	{
		/*
		this.mwl.initialize();
		for(Word w : this.mwl)
		{
			if(this.wc.getName(w.getIdentity()).equals("hans") || this.wc.getName(w.getIdentity()).equals("sara"))
			{this.mwl.setTag(w, 0);}
		}*/
		ivt.train(this.mwl, 100.0, 0);
		StringWriter sw = new StringWriter();
		BufferedWriter bw = new BufferedWriter(sw);
		this.mwl.writeOutTags(bw);
		bw.close();
		System.out.println("Deutsch");
		for(Word w : this.mwl)
		{
			if(this.wc.getName(w.getLanguage()).equals("DEUTSCH"))
			{
				System.out.println(wc.getName(w.getIdentity()));
				System.out.println(this.mwl.getTag(w));
			}
		}
		
		//TODO
	}
}